//
//  RTJQuickPayController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/29.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJQuickPayController.h"

@interface RTJQuickPayController ()

@property (weak, nonatomic) IBOutlet UIButton *sendSMSBtn;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UITextField *codeTxt;

@property (weak, nonatomic) IBOutlet UIButton *finishBtn;

@end

@implementation RTJQuickPayController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *phone = [NSString stringWithFormat:@"%@",_dic[@"mobile"]];
    
     _phoneLab.text = [Apputil replaceStringWithAsterisk:phone startLocation:3 lenght:phone.length -7];
    
    self.sendSMSBtn.layer.borderColor = [UIColor colorForHex:@"#e5e5e5"].CGColor;
    self.sendSMSBtn.layer.borderWidth = 1;
    self.sendSMSBtn.layer.cornerRadius = 5;
    self.sendSMSBtn.layer.masksToBounds = YES;
    [self.sendSMSBtn startWithTime: 60 title:@"重新获取验证码" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];

}

-(void)autoSendSMS{
    
    [RTJNetWork asyncRequestWithURL:@"user/get_code_for_kq" params:_dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.order_sn = [NSString stringWithFormat:@"%@",data[@"data"][@"order_sn"]];
                [SVProgressHUD showSuccessWithStatus:@"验证短信已经发送，请注意查收"];
                [self.sendSMSBtn startWithTime: 60 title:@"重新获取验证码" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
        
    }];
}

- (IBAction)sendSMS:(id)sender {
    
    [self autoSendSMS];

}

- (IBAction)finish:(id)sender {
    
    if (_codeTxt.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    NSDictionary *dic = @{@"amount":_money,
                          @"code":_codeTxt.text,
                          @"order_sn":_order_sn
                          };
    
    [RTJNetWork asyncRequestWithURL:@"user/purchase" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [SVProgressHUD showSuccessWithStatus:@"支付成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
        }
        
    }];
    
}


@end
